<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019/5/23
 * Time: 17:42
 */
namespace app\admin\controller;
use app\admin\model\UpFile;
use think\Controller;
use app\admin\model\Enclosure as Enclosures;
use think\Request;

class Enclosure extends Controller
{
    /**加载表格
     * @return mixed
     */
    public function show_list(){
        return NewBuilder::make('table')
            ->addTableHeaderAllDel('enclosure/del','全部删除')
            ->addTableHeaderAdd('enclosure/show_add','添加')
            ->addTableHeaderSousuo('','搜索')
            ->addTableColumn('id','text','ID')
            ->addTableColumn('yuan_name','edit','原名称',null,'enclosure/feildedit')
            ->addTableColumn('new_name','edit','新名称',null,'enclosure/feildedit')
            ->addTableColumn('tupian','img','图')
            ->addTableColumn('path','edit','相对路径',null,'enclosure/feildedit')
//            ->addTableColumn('quan_path','edit','绝对路径',null,'enclosure/feildedit')
            ->addTableColumn('create_time','text','创建时间')
            ->addTableColumn('status','switch','状态',['开启','禁止'],'enclosure/change')
            ->addTableRightHeader('操作')
//            ->addTableRight('detail','enclosure/detail','查看')
            ->addTableRight('del','enclosure/del','删除')
            ->addTablePage('enclosure/page')
            ->fetch();
    }

    /**加载数据
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function page(Request $request){
        $tiaojian = $request->param('tiaojian',null);
        $limit = $request->param('limit',10);
        if($tiaojian){
            $data = Enclosures::where('id','>',0)
                ->where('yuan_name|new_name|id|path','like',"%$tiaojian%")
                ->order('create_time desc')
                ->paginate($limit)
                ->toArray();
        }else{
            $data = Enclosures::where('id','>',0)
                ->order('create_time desc')
                ->paginate($limit)
                ->toArray();
        }
        foreach ($data['data'] as $key=>$value){
            $type = explode('.',$value['new_name']);
            if(in_array($type[1],['jpg','jpeg','png','gif'])){
                $value['tupian'] = getFilePath($value['id'],true,false);
            }else{
                $value['tupian'] = getFilePath(0,true,false);
            }

            $data['data'][$key] = $value;
        }
        return json_page_response($data);
    }

    /**修改状态
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function change(Request $request){
        $id = $request->param('id');
        $data = Enclosures::where('id',$id)->findOrEmpty()->toArray();
        if($data){
            if($data['status']==1){
                $data['status'] = 0;
            }else{
                $data['status'] = 1;
            }
            if(Enclosures::where('id',$id)->update([
               'status'=>$data['status']
            ])){
                return json_my_response('修改成功',200);
            }else{
                return json_my_response('修改失败');
            }
        }
    }

    /**加载上传文件
     * @param Request $request
     * @return mixed
     */
    public function show_add(Request $request){
        return NewBuilder::make('form')
            ->addAction('enclosure/add')
            ->addFile('field','文件')
            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
    }

    /**添加
     * @return \think\response\Json
     */
    public function add(){
        return redirect('index/errors',['code'=>200]);
    }

    /**删除附件
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del(Request $request){
        $id = $request->param('id');
        if(UpFile::delFile($id,true)){
            return json_my_response('删除成功',200);
        }else{
            return json_my_response('删除失败');
        }
    }
}